#!/usr/bin/env groovy
// @file     GravicSigninParser.groovy
// @purpose  parser for sign-in records
// @author   Mac Radigan  

package ycoa.scan.signin

import org.codehaus.groovy.scriptom.*
import au.com.bytecode.opencsv.*
import java.text.*
import java.util.TimeZone

import sys.xml.XMLUtilities
import ycoa.xml.*
import ycoa.service.*
import ycoa.session.*
import ycoa.markup.*
import ycoa.scan.*
import ycoa.scan.signin.*
import ycoa.text.*

public class GravicSigninParser extends SigninParser { 
  public recordsetToDocument(signinRecordset) { 
    def document = XMLUtilities.newDocument("<recordset/>")
    signinRecordset.records.each() { record ->
    }
  }
  public printFormattedRecordset(signinRecordset) { 
    println "[listing records]"
    def index = 0
    def present = null
    def makeups = null
    def specialty = null
    def evaluation = null
    def studentid = null
    println "CPID#      STID#     PRESENT  MU#  SPECIAL  EVALUATION"
    println "=========  ========  =======  ===  =======  =========="
    signinRecordset.records.each() { record ->
      if(record.present) {present="P  "} else {present="AB "}
      makeups="  ${record.getMakeups()}"
      if(record.specialty) {specialty="YES"} else {specialty="NO "}
      if(record.evaluation) {specialty="YES"} else {specialty="NO "}
      studentid=String.format("%08d", record.studentID)
      println "${record.classReportID}     ${studentid}  ${present}    ${makeups}    ${specialty}"
    }
  }
  public printRecordset(signinRecordset) { 
    println "[listing records]"
    def index = 0
    signinRecordset.records.each() { record ->
      println "RECORD ${++index}> "
      println "  GLOBAL className ${record.className}"
      println "  GLOBAL formPath  ${record.formPath}"
      println "  GLOBAL formName  ${record.formName}"
      println "  GLOBAL reportID  ${record.reportID}"
      println "  classReportID:   ${record.classReportID}"
      println "  className:       ${record.className}"
      println "  studentID:       ${record.studentID}"
      println "  studentName:     ${record.studentName}"
      println "  feeType:         ${record.feeType}"
      println "  parentName:      ${record.parentName}"
      println "  signature:       ${record.signature}"
      println "  present:         ${record.present}"
      println "  makeup1:         ${record.makeup1}"
      println "  makeup2:         ${record.makeup2}"
      println "  makeup3:         ${record.makeup3}"
      println "  makeupCount:     ${record.getMakeups()}"
      println "  specialty:       ${record.specialty}"
      println "  evaluation:      ${record.evaluation}"
      println "  amountPaid:      ${record.amountPaid}"
    }
  }
  public SigninRecordset parseOutput(xmlfile) { 
    def xmlData = new File(xmlfile).text.replace("#RowsetSchema", "RowsetSchema")
    def doc = XMLUtilities.newDocument(xmlData)
    def node = null
    def signinRecordset = new SigninRecordset()
    def signinRecord = null
    def index = 0
    def record = 0
    def attribute = null
    // each record
    node = XMLUtilities.findSingleNode(doc,"//z:row")
      def reportID = null
      def classReportID = 0 
      def className = ""
      def classDate = new Date()
      def formatter = new SimpleDateFormat("MMM dd. HH:mm a .")
      index = 0
      try {
        reportID = node.getAttribute("ReportID")
      } catch(e) {
        println "record #${index}:  error reading Report ID"
        //e.printStackTrace()
        //System.exit(1)
      }
      try {
        classReportID = Integer.parseInt(node.getAttribute("ClassReportID"))
      } catch(e) {
        println "record #${index}:  error reading Class Report ID"
        //e.printStackTrace()
        //System.exit(1)
      }
      try {
        //className = node.getAttribute(ClassName)
        className = "N/A"
      } catch(e) {
        println "record #${index}:  error reading Class Name"
      }
      index++ // fix
      for(int recordCount=1; recordCount<=16; recordCount++) {
        signinRecord = new SigninRecord()
        signinRecord.reportID = reportID
        signinRecord.classReportID = classReportID
        signinRecord.className = className
        try {
          attribute = "StudentID${recordCount}"
          signinRecord.studentID = Integer.parseInt(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading student ID <...>"
        }
        try {
          attribute = "StudentName${recordCount}"
          //signinRecord.studentName = node.getAttribute(attribute)
          signinRecord.studentName = "N/A"
        } catch(e) {
          println "record #${index}:  error reading student name"
        }
        try {
          attribute = "FeeType${recordCount}"
          //signinRecord.feeType = node.getAttribute(attribute)
          signinRecord.feeType = "N/A"
        } catch(e) {
          println "record #${index}:  error reading fee type"
        }
        try {
          attribute = "ParentName${recordCount}"
          //signinRecord.parentName = node.getAttribute(attribute)
          signinRecord.parentName = "N/A"
        } catch(e) {
          println "record #${index}:  error reading parent name"
        }
        try {
          attribute = "Signature${recordCount}"
          //signinRecord.signature = parseBoolean(node.getAttribute(attribute)) 
          signinRecord.signature = true
        } catch(e) {
          println "record #${index}:  error reading parent signature"
        }
        try {
          attribute = "Present${recordCount}"
          signinRecord.present = parseBoolean(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading present"
        }
        try {
          attribute = "Makeups${0+3*(recordCount-1)}"
          signinRecord.makeup1 = parseBoolean(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading makeup #1"
        }
        try {
          attribute = "Makeups${1+3*(recordCount-1)}"
          signinRecord.makeup2 = parseBoolean(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading makeup #2"
        }
        try {
          attribute = "Makeups${2+3*(recordCount-1)}"
          signinRecord.makeup3 = parseBoolean(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading makeup #3"
        }
        try {
          attribute = "Specialty${recordCount}"
          signinRecord.specialty = parseBoolean(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading speialty"
        }
        try {
          attribute = "Evaluation${recordCount}"
          signinRecord.evaluation = parseBoolean(node.getAttribute(attribute))
        } catch(e) {
          println "record #${index}:  error reading evaluation"
        }
        try {
          attribute = "AmountPaid${recordCount}"
          //signinRecord.amountPaid = parseBoolean(node.getAttribute(attribute))
          signinRecord.amountPaid = true
        } catch(e) {
          println "record #${index}:  error reading amount paid"
        }
        // record summary
        try {
          signinRecord.formPath = "N/A"
        } catch(e) {
          println "record #${index}:  error reading image path"
        }
        try {
          signinRecord.formName = "N/A"
        } catch(e) {
          println "record #${index}:  error reading image path"
        }
        try {
          attribute = "crbarcode"
          signinRecord.barcode = node.getAttribute(attribute)
        } catch(e) {
          println "record #${index}:  error reading barcode"
        }
        signinRecordset.records.add(signinRecord)
    }
    return signinRecordset
  } 
  public boolean parseBoolean(value) {
    if(  value.toLowerCase().startsWith("y") || value.toLowerCase().startsWith("t") || value.toLowerCase().startsWith("1") ) {
      return true
    } else {
      return false
    }
  }
  public boolean verifyRow(row) {
    try {
      return row[0].trim()!=''
    } catch(e) {
      return false
    }
  }
} 

// *EOF*
